package servlet.hou;

import bean.Hou;
import bean.PageRequest;
import bean.Result;
import bean.Student;
import com.fasterxml.jackson.databind.ObjectMapper;
import dao.UserDaoImpl;
import service.UserService;
import service.UserServiceImpl;
import utils.DBUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


//宿管注册
@WebServlet("/hou/sign")
public class HouSignServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("===============宿舍管理员注册页面=============");

        String houName = request.getParameter("houName");
        String password = request.getParameter("password");
        int houId = Integer.parseInt(request.getParameter("houId"));
        String  phone = request.getParameter("phone");
        Integer buildId=Integer.parseInt(request.getParameter("buildId"));
//        int sex=Integer.parseInt(request.getParameter("sex"));
//        int age=Integer.parseInt(request.getParameter("age"));


        List<Hou> lists = findAllHou();
        for(Hou hou:lists)
        {
            if(houId == hou.getHouId())
            {
                Result result= new Result();
                result.fail("工号冲突");
                ObjectMapper objectMapper = new ObjectMapper();
                String str = objectMapper.writeValueAsString(result);
                response.setContentType("application/json;charset=utf8");
                response.getWriter().print(str);
                return;
            }
        }
        UserService userService = new UserServiceImpl();
        Hou hou =new Hou();
        hou.setHouName(houName);
        hou.setPassword(password);
        hou.setPhone(phone);
        hou.setBuildId(buildId);
        hou.setHouId(houId);
        try {
            hou = userService.houIsSignSuccess(hou);
            System.out.println(hou);
        }catch (Exception e) {
            Result result= new Result();
            result.fail(e.toString());
            ObjectMapper objectMapper = new ObjectMapper();
            String str = objectMapper.writeValueAsString(result);
            response.setContentType("application/json;charset=utf8");
            response.getWriter().print(str);
        }
        Result result = new Result();
        ObjectMapper objectMapper = new ObjectMapper();
        hou = new UserDaoImpl().houFindByPasswordAndId(password,houId);

        result.success(hou);
        String str = objectMapper.writeValueAsString(result);
        response.setContentType("application/json;charset=utf8");
        response.getWriter().print(str);







    }

    private List<Hou> findAllHou() {
        PageRequest pageRequest = new PageRequest();
        Connection connection=null;
        ResultSet resultSet = null;
        PreparedStatement preparedStatement = null;

        try {
            connection = DBUtil.getConnection();
            String sql = "select * from hou where  delete_flag=0 ";
            preparedStatement = connection.prepareStatement(sql);

            resultSet = preparedStatement.executeQuery();
            List<Hou> hols = new ArrayList<>();
            while (resultSet.next()){
                Hou hou=new Hou();
                hou.setHouId(resultSet.getInt("hou_id"));

                hols.add(hou);

            }

            return hols;


        } catch (SQLException e) {
            System.out.println(e);
            throw new RuntimeException(e);
        }
    }

}
